package han.bashe.web.servlet;

import han.bashe.business.ejb.Pipeline;
import han.bashe.common.converter.ServletReqResHeler;
import han.bashe.common.converter.Soap11Converter;
import han.bashe.common.converter.Soap12Converter;
import han.bashe.common.entity.BasheEntityConstants;
import han.bashe.common.exception.BasheException;
import han.bashe.common.message.NormalizedMessage;
import han.bashe.common.message.RequestHelper;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Servlet implementation class for Servlet: SoapServlet
 * 
 */
public class SoapServlet extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	 @EJB
	   private Pipeline pipeline;
	 
	 private static Log log = LogFactory.getLog(SoapServlet.class);
	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public SoapServlet() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		boolean soap11 = false;
		
		try {
			String xml = ServletReqResHeler.getRequestString(request);
			log.debug("request \n" + xml);
			
			soap11 = Soap11Converter.isSoap11(xml);

			if (soap11) {
				NormalizedMessage msg = Soap11Converter.toNormalized(xml,false);
				
				RequestHelper rh = new RequestHelper(msg);
				rh.setOrginMsgType(BasheEntityConstants.SOAP11_TYPE);
				
				msg = pipeline.process(msg);
				String x = Soap11Converter.toSoap(msg);
				ServletReqResHeler.setResponseString(response, x);
			}else{
				NormalizedMessage msg = Soap12Converter.toNormalized(xml,false);
				
				RequestHelper rh = new RequestHelper(msg);
				rh.setOrginMsgType(BasheEntityConstants.SOAP12_TYPE);
				
				msg = pipeline.process(msg);
				String x = Soap12Converter.toSoap(msg);
				ServletReqResHeler.setResponseString(response, x);
			}
		} catch (BasheException e) {
			Log log = LogFactory.getLog(SoapServlet.class);
			log.error("SoapServlet.doPost() error", e);
			String xml = null;
			if(soap11){
				xml = Soap11Converter.toSoapFault(e);
			}else{
				xml = Soap12Converter.toSoapFault(e);
			}
			ServletReqResHeler.setResponseString(response, xml);

		} catch (Exception e) {
			Log log = LogFactory.getLog(SoapServlet.class);
			log.error("SoapServlet.doPost() error", e);
			String xml = null;
			if(soap11){
				xml = Soap11Converter.toSoapFault(e);
			}else{
				xml = Soap12Converter.toSoapFault(e);
			}
			ServletReqResHeler.setResponseString(response, xml);

		}
	}
}